package com.trkj.usercenter.question;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.trkj.base.BaseDataRefreshFragment;
import com.trkj.base.Constants;
import com.trkj.base.User;
import com.trkj.base.utils.UserService;
import com.trkj.taoruiyun.R;
import com.trkj.widget.refresh.SwipeRefreshLayout;

import java.text.MessageFormat;

/**
 * 我的提问
 * Created by LiZanhong on 2015/06/25.
 */
public class QuestionFragment extends BaseDataRefreshFragment{
    public int getUid() {
        User user= UserService.getInstance(this.getActivity()).parseUser();
        return user == null ? 0 : user.userId;
    }

    @Override
    public String getUrl() {
        String url = MessageFormat.format(
                Constants.ApiUrl.QUESTION,
                getUid(),
                getPage(),
                getRows());
        Log.i("URL", url);
        return url;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //先定义Adapter
        if(getAdapter() == null) {
            setAdapter(new QuestionAdapter());
        }
        View root = super.onCreateView(inflater, container, savedInstanceState);
        return root;
    }

    @Override
    protected SwipeRefreshLayout.Mode getMode() {
        return SwipeRefreshLayout.Mode.BOTH;
    }

    class QuestionAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return getData().size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder viewHolder;

            if(convertView == null){
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_question_item, null);
                viewHolder = new ViewHolder();
                viewHolder.question = (TextView)convertView.findViewById(R.id.question);
                viewHolder.author = (TextView)convertView.findViewById(R.id.author);
                viewHolder.time = (TextView)convertView.findViewById(R.id.time);
                viewHolder.tiMu = (TextView)convertView.findViewById(R.id.ti_mu);
                viewHolder.answer = (TextView)convertView.findViewById(R.id.answer);

                convertView.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder)convertView.getTag();
            }

            JSONObject jsonObject = getData().getJSONObject(position);
            viewHolder.question.setText(Html.fromHtml(jsonObject.getString("ask_desc")));
            viewHolder.author.setText("提问人：" + jsonObject.getString("user_name"));
            viewHolder.time.setText(jsonObject.getString("ask_time"));
            int type = jsonObject.getIntValue("type_");

            String strTimu = jsonObject.getString("question_title");
            if(type == 1){
                //选择题
                strTimu +=  "<br>" + "A、" + jsonObject.getString("A") + "<br>"
                        + "B、" + jsonObject.getString("B") + "<br>"
                        + "C、" + jsonObject.getString("C") + "<br>"
                        + "D、" + jsonObject.getString("D");

            }
            viewHolder.tiMu.setText(Html.fromHtml(strTimu));

            //回答，有多个
            String strAnswer = "";
            JSONArray answers = jsonObject.getJSONArray("answers");
            int len = answers.size();
            for(int i = 0; i < len; i ++){
                JSONObject answer = answers.getJSONObject(i);
                strAnswer += answer.getString("user_name") + "于"
                        + answer.getString("aa_time") + "回答<br>&nbsp;&nbsp;&nbsp;&nbsp;"
                        + answer.getString("content_");
                if(i < len - 1) {
                    strAnswer += "<br>";
                }
            }
            viewHolder.answer.setText(Html.fromHtml(strAnswer));

            return convertView;
        }

        class ViewHolder{
            TextView question;
            TextView author;
            TextView time;
            TextView tiMu;
            TextView answer;
        }
    }
}
